package com.atom.common.remote.palace;

import java.io.Serializable;
import java.util.Date;
import java.util.Map;

import org.activiti.engine.task.Task;

import com.atom.common.TaskCompleteType;
import com.atom.common.VariablesConstant;
import com.atom.common.util.DateUtil;
import com.atom.common.util.PreConditionUtil;
import com.atom.common.util.UrlGenerateUtil;

/**
 * Title: RemoteHistoricTaskDO
 * Description: 平台已办列表
 * @author WEWE
 * @date 2016年6月28日 下午3:32:34
 * @version 1.0
 */
public class RemoteHistoricTaskDO implements Serializable {
  /** serialVersionUID*/
  private static final long serialVersionUID = 1L;
  
  private static final int TRUE = 1;
  
  /** 活动受理人*/
  private String OwnerID;
  
  /** 被代理人*/
  private String PrincipalID;
  
  /** 起草人ID*/
  private String CreateUserID;
  
  /** 起草部门ID*/
  private String CreateDepartID;
  
  /** 前一步操作人ID*/
  private String PreHandlerID;
  
  /** 任务名称*/
  private String TaskName;
  
  /** 公文编号*/
  private String DocNum;
  
  /** 系统模块ID*/
  private String SysModID;
  
  /** 业务类型ID*/
  //private String JobTypeID;
  
  /** 公文类型ID*/
  private String DocTypeID;
  
  /** 公文类型*/
  private String DocTypeName;
  
  /** 工作步骤*/
  private String TaskStepName;
  
  /** 退回标志*/
  private int Rejected;
  
  /** 自动提交标志*/
  private int AutoCommit;
  
  /** 提交时间*/
  private String SubmitTime;
  
  /** 详细信息*/
  private String DetailURL;
  
  /** 流程图*/
  private String FlowChartURL;
  
  /** 操作日志*/
  private String OpLogURL;
  
  /** 主流程实例ID*/
  private String MainProcEntityID;
  
  /** 流程实例ID*/
  private String ProcEntityID;
  
  /** 工作步骤ID*/
  private String TaskStepID;
  
  /** 活动实例ID*/
  private String ActivityEntityID;
  
  /** 流程版本*/
  private String ProcVersion;
  
  /** 是否最新*/
  private int Lasted;  
  
  public RemoteHistoricTaskDO() {
    super();
  }
  
  public static RemoteHistoricTaskDO getRemoteHistoricTaskDO(Task task, Map<String, Object> variables, int completeType) {
    preCheck(task, variables);
    RemoteHistoricTaskDO entity = build(task, variables, completeType);
    return entity;
  }
  
  private static void preCheck(Task task, Map<String, Object> variables) {
    PreConditionUtil.checkNotNull(task, "task is null");
    
    PreConditionUtil.checkNotNull(variables, "variables is null");
    
    PreConditionUtil.checkNotNull(task.getAssignee(), "OwerID is null");
    
    PreConditionUtil.checkNotNull(variables.get(VariablesConstant.PROCESS_CREATOR), "CreateUserID is null");
    
    PreConditionUtil.checkNotNull(variables.get(VariablesConstant.PROCESS_CREATOR_DEPART), "CreateDepartID is null");
    
    PreConditionUtil.checkNotNull(variables.get(VariablesConstant.PROCESS_INSTANCE_NAME), "TaskName is null");
    
    PreConditionUtil.checkNotNull(variables.get(VariablesConstant.PROCESS_NUMBER), "DocNum is null");
    
    PreConditionUtil.checkNotNull(variables.get(VariablesConstant.SYSTEM_MODULER), "SysModID is null");
    
    PreConditionUtil.checkNotNull(variables.get(VariablesConstant.PROCESS_DEFINITION_KEY), "DocTypeID is null");
    
    PreConditionUtil.checkNotNull(variables.get(VariablesConstant.PROCESS_DEFINITION_NAME), "DocTypeName is null");
    
    PreConditionUtil.checkNotNull(task.getName(), "TaskStepName is null");
    
    PreConditionUtil.checkNotNull(variables.get(VariablesConstant.IS_REJECT), "isReject is null");
    
    PreConditionUtil.checkNotNull(task.getTaskDefinitionKey(), "TaskStepID is null");
    
    PreConditionUtil.checkNotNull(variables.get(VariablesConstant.MAIN_PROCESS_INSTANCE_ID), "MainProcEntityID is null");
    
    PreConditionUtil.checkNotNull(task.getId(), "ActivityEntityID is null");
    
    PreConditionUtil.checkNotNull(task.getProcessDefinitionId(), "ProcEntityID is null");
  }
  
  private static RemoteHistoricTaskDO build(Task task, Map<String, Object> variables, int completeType) {
    RemoteHistoricTaskDO entity = new RemoteHistoricTaskDO();    
    
    entity.setOwerID(task.getAssignee());
    
    entity.setPrincipalID(task.getOwner());
    
    entity.setCreateUserID((String) variables.get(VariablesConstant.PROCESS_CREATOR));
    
    entity.setCreateDepartID((String) variables.get(VariablesConstant.PROCESS_CREATOR_DEPART));
    
    entity.setPreHandlerID((String) variables.get(VariablesConstant.PREVIOUS_ASSIGNEE));
    
    entity.setTaskName((String) variables.get(VariablesConstant.PROCESS_INSTANCE_NAME));
    
    entity.setDocNum((String) variables.get(VariablesConstant.PROCESS_NUMBER));
    
    entity.setSysModID((String) variables.get(VariablesConstant.SYSTEM_MODULER));
    
    entity.setDocTypeID((String) variables.get(VariablesConstant.PROCESS_DEFINITION_KEY));
    
    entity.setDocTypeName((String) variables.get(VariablesConstant.PROCESS_DEFINITION_NAME));
    
    entity.setTaskStepName(task.getName());
    
    entity.setRejected((Boolean) variables.get(VariablesConstant.IS_REJECT) == Boolean.TRUE ? 1 : 0);  
    
    entity.setAutoCommit(completeType == TaskCompleteType.AUTO ? 1 : 0);
    
    entity.setSubmitTime(DateUtil.formatTime(new Date()));
    
    entity.setMainProcEntityID((String) variables.get(VariablesConstant.SYSTEM_MODULER) 
        + "_" 
        + (String) variables.get(VariablesConstant.PROCESS_DEFINITION_KEY) 
        + "_" 
        + (String) variables.get(VariablesConstant.MAIN_PROCESS_INSTANCE_ID));
    
    entity.setProcEntityID((String) variables.get(VariablesConstant.SYSTEM_MODULER) 
        + "_" 
        + (String) variables.get(VariablesConstant.PROCESS_DEFINITION_KEY) 
        + "_" 
        + task.getProcessInstanceId());
    
    entity.setTaskStepID(task.getTaskDefinitionKey());
    
    entity.setActivityEntityID((String) variables.get(VariablesConstant.SYSTEM_MODULER) 
        + "_" 
        + (String) variables.get(VariablesConstant.PROCESS_DEFINITION_KEY) 
        + "_" 
        + task.getId());
    
    entity.setLasted(TRUE);
    
    entity.setDetailURL(UrlGenerateUtil.getHistoryUrl( 
        (String) variables.get(VariablesConstant.PROCESS_DEFINITION_KEY), 
        task.getId()));
    
    entity.setFlowChartURL(UrlGenerateUtil.getDiagramUrl(task.getTaskDefinitionKey(), 
        (String) variables.get(VariablesConstant.PROCESS_DEFINITION_KEY), 
        (String) variables.get(VariablesConstant.MAIN_PROCESS_INSTANCE_ID)));
    
    entity.setOpLogURL(UrlGenerateUtil.getLogUrl((String) variables.get(VariablesConstant.MAIN_PROCESS_INSTANCE_ID),
        (String) variables.get(VariablesConstant.PROCESS_DEFINITION_KEY)));
    
    return entity;
  }

  public String getOwnerID() {
    return OwnerID;
  }
  public void setOwerID(String ownerID) {
    OwnerID = ownerID;
  }
  public String getPrincipalID() {
    return PrincipalID;
  }
  public void setPrincipalID(String principalID) {
    PrincipalID = principalID;
  }
  public String getCreateUserID() {
    return CreateUserID;
  }
  public void setCreateUserID(String createUserID) {
    CreateUserID = createUserID;
  }
  public String getCreateDepartID() {
    return CreateDepartID;
  }
  public void setCreateDepartID(String createDepartID) {
    CreateDepartID = createDepartID;
  }
  public String getPreHandlerID() {
    return PreHandlerID;
  }
  public void setPreHandlerID(String preHandlerID) {
    PreHandlerID = preHandlerID;
  }
  public String getTaskName() {
    return TaskName;
  }
  public void setTaskName(String taskName) {
    TaskName = taskName;
  }
  public String getDocNum() {
    return DocNum;
  }
  public void setDocNum(String docNum) {
    DocNum = docNum;
  }
  public String getSysModID() {
    return SysModID;
  }
  public void setSysModID(String sysModID) {
    SysModID = sysModID;
  }
  public String getDocTypeID() {
    return DocTypeID;
  }
  public void setDocTypeID(String docTypeID) {
    DocTypeID = docTypeID;
  }
  public String getDocTypeName() {
    return DocTypeName;
  }
  public void setDocTypeName(String docTypeName) {
    DocTypeName = docTypeName;
  }
  public String getTaskStepName() {
    return TaskStepName;
  }
  public void setTaskStepName(String taskStepName) {
    TaskStepName = taskStepName;
  }
  public int getRejected() {
    return Rejected;
  }
  public void setRejected(int rejected) {
    Rejected = rejected;
  }  
  public int getAutoCommit() {
    return AutoCommit;
  }
  public void setAutoCommit(int autoCommit) {
    AutoCommit = autoCommit;
  }
  public String getSubmitTime() {
    return SubmitTime;
  }
  public void setSubmitTime(String submitTime) {
    SubmitTime = submitTime;
  }
  public String getDetailURL() {
    return DetailURL;
  }
  public void setDetailURL(String detailURL) {
    DetailURL = detailURL;
  }
  public String getFlowChartURL() {
    return FlowChartURL;
  }
  public void setFlowChartURL(String flowChartURL) {
    FlowChartURL = flowChartURL;
  }
  public String getOpLogURL() {
    return OpLogURL;
  }
  public void setOpLogURL(String opLogURL) {
    OpLogURL = opLogURL;
  }
  public String getMainProcEntityID() {
    return MainProcEntityID;
  }
  public void setMainProcEntityID(String mainProcEntityID) {
    MainProcEntityID = mainProcEntityID;
  }
  public String getProcEntityID() {
    return ProcEntityID;
  }
  public void setProcEntityID(String procEntityID) {
    ProcEntityID = procEntityID;
  }
  public String getTaskStepID() {
    return TaskStepID;
  }
  public void setTaskStepID(String taskStepID) {
    TaskStepID = taskStepID;
  }
  public String getActivityEntityID() {
    return ActivityEntityID;
  }
  public void setActivityEntityID(String activityEntityID) {
    ActivityEntityID = activityEntityID;
  }
  public String getProcVersion() {
    return ProcVersion;
  }
  public void setProcVersion(String procVersion) {
    ProcVersion = procVersion;
  }
  public int getLasted() {
    return Lasted;
  }
  public void setLasted(int lasted) {
    Lasted = lasted;
  }

}
